package com.pricl.wechat.faq.controller;

import java.util.ArrayList;
import java.util.List;

import com.soecode.wxtools.api.IService;
import com.soecode.wxtools.api.WxConsts;
import com.soecode.wxtools.api.WxService;
import com.soecode.wxtools.bean.WxMenu;
import com.soecode.wxtools.bean.WxMenu.WxMenuButton;
import com.soecode.wxtools.exception.WxErrorException;
/**
 * 微信公众号按钮开发
 * @author winton
 *
 */
public class WeChatMenuController {
	
	private static IService iService = new WxService();
	
	public static void main(String[] args) {
		deleteMenu();
		createMenu();
	}
	
	public static void deleteMenu() {
		try {
            iService.deleteMenu();
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
	}
	
	public static void createMenu() {
		WxMenu menu = new WxMenu();
        List<WxMenuButton> btnList = new ArrayList<>();

        //设置VIEW类型的按钮2
        WxMenuButton btn2 = new WxMenuButton();
        btn2.setType(WxConsts.BUTTON_VIEW);
        btn2.setUrl("http://winton.imwork.net/wechat/faq/index");
        btn2.setName("瓜姐知道");

        //设置含有子按钮的按钮3
        List<WxMenuButton> subList = new ArrayList<>();
        //子按钮
        WxMenuButton btn3_1 = new WxMenuButton();
        btn3_1.setType(WxConsts.BUTTON_VIEW);
        btn3_1.setUrl("http://www.baidu.com");
        btn3_1.setName("瓜姐百科");
        WxMenuButton btn3_2 = new WxMenuButton();
        btn3_2.setType(WxConsts.BUTTON_VIEW);
        btn3_2.setUrl("http://www.baidu.com");
        btn3_2.setName("涨姿势");
        subList.add(btn3_1);
        subList.add(btn3_2);
        //把子按钮列表设置进按钮3
        WxMenuButton btn3 = new WxMenuButton();
        btn3.setName("瓜姐招牌");
        btn3.setSub_button(subList);

        //将三个按钮设置进btnList
        btnList.add(btn2);
        btnList.add(btn3);
        //设置进菜单类
        menu.setButton(btnList);
        //调用API即可
        try {
            //参数1--menu  ，参数2--是否是个性化定制。如果是个性化菜单栏，需要设置MenuRule
            iService.createMenu(menu, false);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
	}
	
}
